package com.chenjj.java.test.common;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    transient String [] table;

    @Test
    public void test1(){
        System.out.println(new Random(1).nextDouble()*10000);
    }


    /**
     * 日期格式的识别
     * 把 yyyy-MM-dd的日期格式转换成yyyy/MM/dd
     */
    @Test
    public void test2(){

        String [] value = {"2019-1-2","2019-01-02","2019-1-02","2019-01-2","2019/01/02","date","    "};
        String formatter = "yyyy-MM-dd";
        DateFormat dateFormat = new SimpleDateFormat(formatter);

         for (int i=0 ;i < value.length; i++) {
             try {
                 dateFormat.parse(value[i]);

                 String transform = value[i].replace("-", "/");
                 System.out.println(value[i] + " -> " + transform + "成功");
             } catch (ParseException e) {
                 System.out.println(value[i] + "不是yyyy-MM-dd日期格式");
             }
         }
    }


    /**
     * 去尾空格
     */
    @Test
    public void test3(){
        String value = "";

        int len= value.length();
        char[] val =value.toCharArray();
        while ((0 < len) && (val[len -1] <= ' ')){
            len --;
        }
        String result = (len < value.length()) ? value.substring(0,len): value;

        System.out.println(result);
    }

    /**
     * 去掉小数里面无效的0，但至少保留1位小数。例如123.00000 -> 123.0    123.456000 ->123.456    123 -> 123.0
     */
    @Test
    public void test4(){
        String [] values = {"123.000000","123.456000","123","123.0","123.00","123.1","a","1234.555666","123.0102","123.01020","10000","000111000","0","92a.3"};

        for (String value: values) {
            //判断是不是数字
            String rex = "^[0-9]\\d*(\\.\\d+)?$";
            Pattern p = Pattern.compile(rex);
            Matcher m = p.matcher(value);
            if (m.find()){
                boolean isInt = false;
                try{
                    Integer.parseInt(value);
                    isInt = true;
                }catch (Exception e){}
                String result = "";
                if (isInt){
                    //是整数，补位
                    result = value + ".0";
                }else{
                    //是小数，从右往左，碰到0就删，直到碰到非0数字
                    int len= value.length();
                    char[] val =value.toCharArray();
                    while ((0 < len) && (val[len -1] == '0')){
                        len --;
                    }
                    //如果最后一位是小数点，则往后多保留一位
                    if (val[len-1] == '.'){
                        len = len +1;
                    }
                    result = (len < value.length()) ? value.substring(0,len): value;
                }
                System.out.println(value + "->" + result);
            }else{
                System.out.println(value+"不是数字");
            }
        }
    }


    /**
     * 测试精度丢失的问题
     */
    @Test
    public void test6(){
        double a = 0.30;
        double b = 0.10;
        System.out.println(a-b);

        BigDecimal a1 = new BigDecimal("0.3");
        BigDecimal b1 = new BigDecimal("0.1");
        System.out.println(a1.subtract(b1));
    }

    /**
     *
     * 测试以下代码的不同？
     *
     *
     * 问题代码：
     * 	String charsetString = new String(rowText.getBytes(),  charsetName);
     *
     * 没问题代码：
     * 	String charsetString = new String(rowText.getBytes(), 0, rowText.getLength(), charsetName);
     *
     *
     * 	结论： 从源码层面没看出不同的地方，除非
     */
    @Test
    public void test7() throws UnsupportedEncodingException {
        String rowText = "你好";
        String a = new String(rowText.getBytes(),  "utf-8");
        String b = new String(rowText.getBytes(), 0, rowText.getBytes().length, "utf-8");
    }

    /**
     * 测试 stirng.split的
     *
     * 预期：~a~b~~~~ -> [a,b, , , ]
     * 实际：~a~b~~~~ -> [a,b]     -> 使用 .split("~",-1) 可以解决
     */
    @Test
    public void test8(){
        String s = "~a~b~~~~";
        String [] result = s.split("~",-1);
        System.out.println(result.length);
        System.out.println(Arrays.toString(result));
    }

    @Test
    public void parseDate() throws ParseException {
        String dateFormat = "yyyyMMdd";
        String strDate = "20230231";
        SimpleDateFormat inputFormat = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        inputFormat.setLenient(false);
        Date parsedDate = inputFormat.parse(strDate);
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(outputFormat.format(parsedDate));

    }
}
